package com.marver.sys.service;

import com.github.pagehelper.PageInfo;
import com.marver.beans.UserInfoQo;
import com.marver.common.Audience;
import com.marver.common.Global;
import com.marver.common.annotation.DataFilter;
import com.marver.common.dao.BaseDAOImpl;
import com.marver.common.exception.HmException;
import com.marver.common.result.Page;
import com.marver.common.result.Result;
import com.marver.common.result.ResultStatus;
import com.marver.common.utils.DateUtils;
import com.marver.common.utils.JedisUtils;
import com.marver.common.utils.JwtHelper;
import com.marver.common.utils.StringUtils;
import com.marver.entity.ByPage;
import com.marver.entity.qo.Param;
import com.marver.entity.sys.User;
import com.marver.shiro.ShiroUtils;
import com.marver.sys.mapper.UserMapper;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @description: 用户service层处理
 * @Author: Marver
 * @date: 2018-04-26 09:50
 **/
@Service
public class UserService extends BaseDAOImpl{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private Audience audienceEntity;

    /**
     * 查询用户所有权限
     * @param userId
     * @return
     */
    public List<String> queryAllPerms(Long userId){
        return (List<String>) this.listBySqlMap("user.queryAllPerms", userId);
    }

    /**
     * 查询用户信息
     * @param user
     * @return
     */
    public User queryUserInfo(User user){
        return userMapper.selectOne(user);
    }

    /**
     * 保存用户
     * @param qo
     */
    public void saveUser(UserInfoQo qo){
        User user = new User();
        BeanUtils.copyProperties(qo,user);
        user.setCreateDateTime(DateUtils.getCurrDateTime());
        String salt = RandomStringUtils.randomAlphanumeric(20);
        user.setSalt(salt);
        user.setPassword(ShiroUtils.sha256(user.getPassword(),user.getSalt()));
        user.setCreater(String.valueOf(ShiroUtils.getUserId()));
        userMapper.insert(user);

        userRoleService.saveOrUpdate(user.getId(), qo.getRoleIdList());
    }

    /**
     * 创建token
     * @param remoteIp
     */
    public List createToken(String remoteIp) {
        User user = ShiroUtils.getUser();
        //拼装token 设置过期时间
        String token = JwtHelper.createJWT(remoteIp,"", String.valueOf(user.getId()), user.getUsername(),
                3600*1000, audienceEntity.getBase64Secret(),"");
        JedisUtils.set(Global.ACCESSTOKEN+user.getMobile(),token,3600);
        Map map = new HashMap();
        map.put("toke", token);
        List<Map> list = new ArrayList<>();
        list.add(map);
        return list;
    }

    /**
     * 查询所有用户列表
     * @param qo
     * @return
     */
    @DataFilter(subDept = true, user = false, userId = "id")
    public Result queryUserList(Param qo){
        User user = new User();
        BeanUtils.copyProperties(qo,user);
        Example example = new Example(user.getClass());
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(user);
        if(StringUtils.isNotEmpty(qo.getSqlFilter())){
            criteria.andCondition(qo.getSqlFilter());
        }
        List<User> userList = userMapper.selectByExample(example);
        if(ByPage.class.isAssignableFrom(qo.getBody().getClass())) {
            PageInfo p = new PageInfo(userList);
            Page page = new Page();
            BeanUtils.copyProperties(p, page);
            return new Result(ResultStatus.SUCCESS.getErrorCode(), userList, page);
        }else {
            throw new HmException("此方法第二个参数必须继承ByPage");
        }
    }
}
